var express = require('express');
var router = express.Router();
var models=require('../../models/models');
var co = require('co');

/**
 * Created by on 2017/10/28.
 */
router.get('/',(req, res)=>{
  if(!req.query.id)req.query.id=0
  if(!req.query.pagesize)req.query.pagesize=15
  if(!req.query.pagenum)req.query.pagenum=1
  let redismodel=models.getModel('articlemodel')
  Promise.all([redismodel.getArticleIdArr(req.query.id),redismodel.getArticleCount()]).then(result=>{
  	let articleId=[]
	result1=result[0].slice(req.query.pagesize*(req.query.pagenum-1),req.query.pagesize*req.query.pagenum)
	result1.map(r=>articleId.push(redismodel.getArticleInfor(r)))
	Promise.all(articleId).then(result2=>{
		let comArr=[]
		result2.map(r=>comArr.push(redismodel.getCommentsInfor(r.comments_id)))
		Promise.all(comArr).then(result3=>{ //评论
			for (var i = 0; i < result1[0].length; i++) {
				result2[i].comments=result3[i]
			}
			res.status(200).json({artle:result2,count:result[1]})
		})
	})
  })
})


module.exports = router;